from django.shortcuts import render
from django.views import View
# Create your views here.
#1.首页广告
class IndexView(View):
    #1.显示商品分类
    def get(self, request):
        from apps.contents.utils import get_categories #注意,此处必须在函数内部导包
        categories = get_categories()
        #2.显示广告类别
        #2.1 获取所有广告分类 content_category
        from apps.contents.models import ContentCategory
        content_categories = ContentCategory.objects.all()
        #2.2 遍历广告分类
        contents = dict()
        for cat in content_categories:
        #2.3 通过外键属性,获取广告内容
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')
        #前端内容
        context = {
            'categories':categories,
            'contents':contents
        }
        return render(request, 'index.html', context)